﻿var roleService = (function ()
{
    var roleservice = {
        editIndex:-1,
        oInit: function ()
        {
            $('#dgRoles').datagrid({
                //url: 'datagrid_data.json',
                height: 350,
                //width: 1200,
                idField:"id",
                singleSelect: true,
                autoRowHeight: true,
                //onClickCell: roleService.onClickCell,
                pagination: true,
                pageSize: 10,
                columns: [
                    [
                        { field: 'name', title: '角色名',editor:"validatebox", width: 120 },
                        { field: 'displayName', title: '角色显示名称',editor:"text", width: 120 },
                         { field: 'creatorUserName', title: '创建人', editor: "text", width: 120 },
                         { field: 'creationTime', title: '创建时间', width: 200, editor: "datetimebox", align: 'right' },
                         { field: 'lastModifierUserName', title: '修改人', editor: "text", width: 120 },
                        { field: 'lastModificationTime', title: '修改时间', width: 200, editor: "datetimebox", align: 'right' }
                    ]
                ],
                toolbar: [
                    {
                        text: '新增',
                        iconCls: 'icon-add',
                        handler: function() {
                            roleService.append();
                        }
                    }, '-', {
                        text: '保存',
                        iconCls: 'icon-save',
                        handler: function() {
                            roleService.endEditing();
                        }
                    }, '-', {
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: function() {
                            alert('修改');
                        }
                    }, '-', {
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: function() {
                            alert('删除');
                        }
                    }

                ]
            });
            roleService.loadData();
        },
        loadData: function ()
        {
            abp.ui.setBusy(
                $('#dgRoles'),
                abp.services.app.role.getAllRoles().done(function (roles)
                {
                    var data = { rows: roles.items, total: roles.items.length };
                    $('#dgRoles').datagrid('loadData', data);
                })
            );
        },
        getData:function() {
            abp.ui.setBusy(
                $('#dg'),
                abp.services.app.role.getAllRoles().done(function (roles)
                {
                    var data = { rows: roles.items, total: roles.items.length };
                    return data;
                })
            );
        },
        endEditing:function() {
            if (roleService.editIndex === -1) { return true }
            if ($('#dgRoles').datagrid('validateRow', roleService.editIndex))
            {
                var ed = $('#dgRoles').datagrid('getEditor', { index: roleService.editIndex });
               
             
                $('#dgRoles').datagrid('endEdit', roleService.editIndex);
                roleService.editIndex = -1;
                return true;
            } else
            {
                return false;
            }

        },
        append:function() {
            if (roleService.endEditing())
            {
                $('#dgRoles').datagrid('appendRow', {});
                roleService.editIndex = $('#dgRoles').datagrid('getRows').length-1;
                $('#dgRoles').datagrid('selectRow', roleService.editIndex);
                $('#dgRoles').datagrid('beginEdit', roleService.editIndex);
            }

        },
        onClickCell: function(index, field) {
            if (roleService.editIndex !== index)
            {
                if (roleService.endEditing())
                {
                    $('#dgRoles').datagrid('selectRow', index)
							.datagrid('beginEdit', index);
                    var ed = $('#dgRoles').datagrid('getEditor', { index: index, field: field });
                    if (ed)
                    {
                        ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
                    }
                    roleService.editIndex = index;
                } else
                {
                    $('#dgRoles').datagrid('selectRow', roleService.editIndex);
                }
            }

        },
        /**
         * 保存角色
         * @returns {} 
         */
        onSave: function () {
            $('#dg').edatagrid('saveRow');
            var row = $('#dg').datagrid("getChanges");
             var roleInput = {
                 RoleName: row[0].name,
                 DisplayName:row[0].displayName
             }
             abp.services.app.role.createRole(roleInput).done(function (resut)
             {
                $('#dg').datagrid('reload');
            });
        },
        /**
         * 删除角色
         * @returns {} 
         */
        onDelete:function() {
            $('#dg').edatagrid('destroyRow');
            var row = $('#dg').datagrid("getChanges");
            var roleInput = {
                RoleName: row[0].name,
                DisplayName: row[0].displayName
            }
            abp.ui.setBusy(
               $('#dg'),
               abp.services.app.role.DeleteRoleByRoleId(row.id).done(function (result)
               {
                   $('#dg').datagrid('reload');
               })
           );
        },
        /**
         * 重新加载角色
         * @returns {} 
         */
        onReload:function() {
            $("#dg").datagrid("reload");
        }
    };
    return roleservice;
})();

$.extend($.fn.datagrid.defaults.editors, {
    text: {
        init: function (container, options)
        {
            var input = $('<input type="text" class="datagrid-editable-input">').appendTo(container);
            return input;
        },
        destroy: function (target)
        {
            $(target).remove();
        },
        getValue: function (target)
        {
            return $(target).val();
        },
        setValue: function (target, value)
        {
            $(target).val(value);
        },
        resize: function (target, width)
        {
            $(target)._outerWidth(width);
        }
    }
});